package com.sakura.mall.member.memberCollectSpu.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.member.memberCollectSpu.domain.entity.MemberCollectSpuEntity;
import com.sakura.mall.member.memberCollectSpu.domain.request.AddMemberCollectSpuRequest;
import com.sakura.mall.member.memberCollectSpu.domain.request.UpdateMemberCollectSpuRequest;
import com.sakura.mall.member.memberCollectSpu.domain.request.MemberCollectSpuPageRequest;
import com.sakura.mall.member.memberCollectSpu.domain.vo.MemberCollectSpuVo;
import com.sakura.mall.member.memberCollectSpu.mapper.MemberCollectSpuMapper;
import com.sakura.mall.member.memberCollectSpu.service.MemberCollectSpuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员收藏的商品 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class MemberCollectSpuServiceImpl extends ServiceImpl<MemberCollectSpuMapper, MemberCollectSpuEntity> implements MemberCollectSpuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddMemberCollectSpuRequest request) {
        // todo 其他校验逻辑
        MemberCollectSpuEntity memberCollectSpu = request.convert(MemberCollectSpuEntity.class);
        this.save(memberCollectSpu);
    }

    @Override
    public Page<MemberCollectSpuVo> queryPage(MemberCollectSpuPageRequest request) {
        IPage<MemberCollectSpuEntity> qp = request.toPage();
        LambdaQueryWrapper<MemberCollectSpuEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<MemberCollectSpuVo> ret = qp.convert(entity -> entity.convert(MemberCollectSpuVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateMemberCollectSpuRequest request) {
        MemberCollectSpuEntity entity = request.convert(MemberCollectSpuEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

